package com.xb.service;

import com.xb.dao.UserDao;
import com.xb.entity.User;
import com.xb.utils.PageEntity;

import java.util.List;

public class UserService {
    private UserDao userDao=new UserDao();

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    /**
     * 根据email查询用户
     * @param email
     * @return
     */

    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    /**
     * 用户注册
     * @param user
     */
    public void register(User user) {

        userDao.register(user);
    }


    /**
     * 查询用户
     * @return
     */
    public List<User> findAll() {
        return userDao.findAll();
    }

    /**
     *搜索用户
     * @return
     */
    public List<User> search(String searchVal) {
        return userDao.search(searchVal);
    }


    /**
     * 用户搜索加分页
     * @param currPage
     * @param realName
     * @return
     */
    public PageEntity<User> findPage(Integer currPage, String realName) {
        //创建分页对象
        PageEntity<User> pageData=new PageEntity<>();
        //页大小
        Integer pageSize=pageData.getPageSize();

        //封装分页对象


        //查询当前页的数据
        Integer startIndex=(currPage - 1)*pageSize;
        List<User> userList= userDao.findPage(realName,startIndex,pageSize);

        //总记录数
        Integer totalCount= userDao.countByRealName(realName);


        //总页数
        Integer totalPage=(totalCount % pageSize)==0?(totalCount / pageSize) :(totalCount / pageSize)+1;

        pageData.setCurrPage(currPage);
        pageData.setData(userList);
        pageData.setTotalCount(totalCount);
        pageData.setTotalPage(totalPage);

        return pageData;
    }

    /**
     * 根据部门Id去查询用户信息
     * @param deptId
     * @return
     */
    public List<User> findByDeptId(Long deptId) {

        return userDao.findByDeptId(deptId);
    }

    /**
     * 根据id查询用户详情
     * @param userId
     * @return
     */
    public User findById(Long userId) {
        return userDao.findById(userId);
    }


    /**
     * 查询指定用户的粉丝数
     * @param userId
     * @return
     */
    public Integer findFansCount(Long userId) {
        return userDao.findFansCount(userId);
    }


    /**
     * 更新被看数
     * @param userId
     */
    public void updateLook(Long userId) {
        userDao.updateLook(userId);
    }


    /**
     * 查询当前登录用户的关注数
     * @param id
     * @return
     */
    public Integer findFocusCount(Long id) {
        return userDao.findFocusCount(id);
    }


    /**
     * 更新用户信息
     * @param loginUser
     */
    public void update(User loginUser) {
        userDao.update(loginUser);
    }


    /**
     * 更新图片地址
     * @param id
     * @param url
     */
    public void updataPic(Long id, String url) {
        userDao.updataPic(id,url);
    }


    /**
     * 根据id查询这个用户的关注列表
     * @param id
     * @return
     */
    public List<Long> findFocusIdList(Long id) {
        return userDao.findFocusIdList(id);
    }


    /**
     * 我的关注列表+分页
     * @param id
     * @param currPage
     * @return
     */
    public PageEntity<User> findFocusPage(Long id, Integer currPage) {
        //创建分页对象
        PageEntity<User> pageEntity=new PageEntity<>();
        //页大小
        Integer pageSize=pageEntity.getPageSize();


        //查询当前页的数据
        Integer startIndex=(currPage - 1)*pageSize;
        List<User> userList= userDao.findFocusPage(id,startIndex,pageSize);

        //总记录数
        Integer totalCount= userDao.findFocusCount(id);

        //总页数
        Integer totalPage=(totalCount % pageSize)==0?(totalCount / pageSize) :(totalCount / pageSize)+1;

        pageEntity.setData(userList);
        pageEntity.setTotalPage(totalPage);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setCurrPage(currPage);



        return pageEntity;
    }


    /**
     * 判断当前用户是否关注过该用户
     * @param id
     * @param focusId
     * @return
     */
    public Boolean isFocus(Long id, long focusId) {
        Integer count= userDao.countByUserIdAndFocusId(id,focusId);
        return count>0;
    }


    /**
     * 取消关注
     * @param id
     * @param focusId
     */
    public void delFocus(Long id, long focusId) {
        userDao.deleteByUserIdAndFocusId(id,focusId);
    }


    /**
     * 添加关注
     * @param id
     * @param focusId
     */
    public void addFocus(Long id, long focusId) {
        userDao.addByUserIdAndFocusId(id,focusId);
    }


    /**
     * 根据微信颁发的openid查询用户
     * @param openid
     * @return
     */
    public User findByOpenId(String openid) {
        return userDao.findByOpenId(openid);
    }
}
